home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / strlib.zip / INT2STR.C < prev    next >
Text File  |  1993-01-04  |  3KB  |  68 lines

  1.  
  2. /*  File   : int2str.c
  3.     Author : Richard A. O'Keefe
  4.     Updated: 30 April 1984
  5.     Defines: int2str(), itoa(), ltoa()
  6.  
  7.     int2str(dst, radix, val)
  8.     converts the (long) integer "val" to character form and moves it to
  9.     the destination string "dst" followed by a terminating NUL.  The
  10.     result is normally a pointer to this NUL character, but if the radix
  11.     is dud the result will be NullS and nothing will be changed.
  12.  
  13.     If radix is -2..-36, val is taken to be SIGNED.
  14.     If radix is  2.. 36, val is taken to be UNSIGNED.
  15.     That is, val is signed if and only if radix is.  You will normally
  16.     use radix -10 only through itoa and ltoa, for radix 2, 8, or 16
  17.     unsigned is what you generally want.
  18.  
  19.     _dig_vec is public just in case someone has a use for it.
  20.     The definitions of itoa and ltoa are actually macros in strings.h,
  21.     but this is where the code is.
  22. */
  23.  
  24. #include "strings.h"
  25.  
  26. char _dig_vec[] =
  27.     "0123456789abcdefghijklmnopqrstuvwxyz";
  28.  
  29.  
  30. char *int2str(dst, radix, val)
  31.     register char *dst;
  32.     register int radix;
  33.     register long val;
  34.     {
  35.         char buffer[33];
  36.         register char *p;
  37.  
  38.         if (radix < 0) {
  39.             if (radix < -36 || radix > -2) return NullS;
  40.             if (val < 0) {
  41.                 *dst++ = '-';
  42.                 val = -val;
  43.             }
  44.             radix = -radix;
  45.         } else {
  46.             if (radix > 36 || radix < 2) return NullS;
  47.         }
  48.         /*  The slightly contorted code which follows is due to the
  49.             fact that few machines directly support unsigned long / and %.
  50.             Certainly the VAX C compiler generates a subroutine call.  In
  51.             the interests of efficiency (hollow laugh) I let this happen
  52.             for the first digit only; after that "val" will be in range so
  53.             that signed integer division will do.  Sorry 'bout that.
  54.             CHECK THE CODE PRODUCED BY YOUR C COMPILER.  The first % and /
  55.             should be unsigned, the second % and / signed, but C compilers
  56.             tend to be extraordinarily sensitive to minor details of style.
  57.             This works on a VAX, that's all I claim for it.
  58.         */
  59.         p = &buffer[32];
  60.         *p = '\0';
  61.         *--p = _dig_vec[(unsigned long)val%(unsigned long)radix];
  62.         val = (unsigned long)val/(unsigned long)radix;
  63.         while (val != 0) *--p = _dig_vec[val%radix], val /= radix;
  64.         while (*dst++ = *p++) ;
  65.         return dst-1;
  66.     }
  67.  
  68.